#include "SoundManager.h"
SoundManager::SoundManager()
{
	m_soundflag = false;
	m_managerflag = false;
	m_start = false;
	m_ismonitor = false;
	m_soundInter = NULL;
	m_soundManagerThread = NULL;
	m_udpApi = NULL;
	m_isplay = false;
	if (!g_get_mutex.IsInitialized())
	{
		if (g_get_mutex.Init()<0)
		{
			printf("g_get_mutex init error!\n");
		}
	}
	if (!g_put_mutex.IsInitialized())
	{
		if (g_put_mutex.Init()<0)
		{
			printf("g_put_mutex init error!\n");
		}
	}
	m_play_mutex.Init();
	g_get_que.clear();
	g_put_que.clear();
}
SoundManager::~SoundManager()
{
	StopThread();
}
int SoundManager::Init(UDPApi *udpApi)
{
	if (udpApi==NULL)
	{
		printf("print udpapi is null!\n");
		return -1;	
	}
	m_udpApi = udpApi;
	return 0;
}
void SoundManager::Process()
{
	RTP_INFO rtpinfo;
	memset(&rtpinfo,0,sizeof(RTP_INFO));
	bool done = false;
	bool put = false;
	if(m_udpApi->GetData(rtpinfo)==0)
	{
		g_get_mutex.Lock();
		m_play_mutex.Lock();
		//if (m_isplay || m_ismonitor)
		{
			g_get_que.push_back(rtpinfo);
		}
		m_play_mutex.Unlock();
		g_get_mutex.Unlock();
		done = true;
	}
	g_put_mutex.Lock();
	if(g_put_que.size()>0)
	{
		
		
		memset(&rtpinfo,0,sizeof(RTP_INFO));
		memcpy(&rtpinfo,&g_put_que.front(),sizeof(RTP_INFO));
		g_put_que.pop_front();
		
		rtpinfo.PayLoadType = 8;
		//printf("payloadlen = %d!\n",rtpinfo.PayLoadLen);
		RTP_INFO t_rtp;
		int num = rtpinfo.PayLoadLen / 320;
		//int num = rtpinfo.PayLoadLen / 160;
		//printf("put net data:%d\n",num);
		for(int i=0; i<num; i++)
		{
			memset(&t_rtp,0,sizeof(RTP_INFO));
			t_rtp.PayLoadType = rtpinfo.PayLoadType;
			t_rtp.PayLoadLen = 320;
			memcpy(t_rtp.DecData, rtpinfo.DecData + 80 * 2 * i, 80 * 4);
			//t_rtp.PayLoadLen = 160;
			//memcpy(t_rtp.DecData,rtpinfo.DecData+160*i,160*sizeof(short));
			if(m_isplay == true&&m_ismonitor == false)
				m_udpApi->PutData(t_rtp);

		}

		put = true;
	}
	g_put_mutex.Unlock();
	if (done ==false && put == false)
	{
		Sleep(1);
	}
}

int SoundManager::StartThread()
{
	int ret = -1;
	if (!m_soundflag)
	{
		m_soundInter = new SoundInterface();
		if (m_soundInter==NULL)
		{
			printf("soundinterface is null!\n");
			return -1;
		}
		m_soundInter->win_init(&g_get_mutex,&g_put_mutex,&g_get_que,&g_put_que);
		ret = m_soundInter->SoundInterfaceStart();
		if (ret < 0)
		{
			printf("ret22=%d\n",ret);
			return -2;
		}
		m_soundInter->SetRead(true);
		m_soundInter->Play();
		m_soundflag = true;
	}
	
	if (!m_managerflag)
	{
		m_soundManagerThread = new SoundManagerThread(*this);
		if (m_soundManagerThread == NULL)
		{
			printf("soundmanager is null!\n");
			return -3;
		}
		ret = m_soundManagerThread->Start();
		if (ret < 0)
		{
			printf("start error! ret = %d\n",ret);
			return -4;
		}
		m_managerflag = true;
	}	
	m_start = true;
	return 0;
}
int SoundManager::StopThread()
{
	if (m_soundflag)
	{
		if (m_soundInter != NULL){
			m_soundInter->SoundInterfaceStop();
			delete m_soundInter;
			m_soundInter = NULL;
		}
	}
	else if (m_soundInter!=NULL)
	{
		m_soundInter->SoundInterfaceStop();
		delete m_soundInter;
		m_soundInter = NULL;
	}
	if (m_managerflag)
	{
		if (m_soundManagerThread != NULL){
			m_soundManagerThread->Stop();
			delete m_soundManagerThread;
			m_soundManagerThread = NULL;
		}
	}
	else if (m_soundManagerThread!=NULL)
	{
		m_soundManagerThread->Stop();
		delete m_soundManagerThread;
		m_soundManagerThread = NULL;
	}
	//zyy change 2017-07-14
	//if (m_udpApi!=NULL)
	//{
	//	delete m_udpApi;
	//	m_udpApi = NULL;
	//}
	//end
	m_soundflag = false;
	m_managerflag = false;
	m_start =false;
	return 0;
}

int SoundManager::SndPlay()
{

	m_isplay = true;
	return 0;
}
int SoundManager::SndStop()
{
	m_isplay = false;
	//add zyy 20170615
	g_get_mutex.Lock();
	m_play_mutex.Lock();
	g_get_que.clear();
	m_play_mutex.Unlock();
	g_get_mutex.Unlock();
	//end
	return 0;
}

int SoundManager::Monitor()
{
	m_ismonitor = true;
	return 0;
}

int SoundManager::CancelMonitor()
{
	m_ismonitor = false;
	//add zyy 20170615
	g_get_mutex.Lock();
	m_play_mutex.Lock();
	g_get_que.clear();
	m_play_mutex.Unlock();
	g_get_mutex.Unlock();
	//end
	return 0;
}

int SoundManager::SoundReset(bool isUp)
{
	if (m_soundInter != NULL)
		return m_soundInter->SoundInterfaceReset(isUp);
	else
		return -1;
}